package com.wokeMy.view;


import android.app.Dialog;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.woke.method.MyApp;
import com.wokeMy.view.base.BaseActivity;
import com.wokeMy.view.model.Constant;
import com.wokeMy.view.model.GhtZd;
import com.wokeMy.view.util.Util;
import com.zhongcai.online.R;

import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;


public class DetailDhActivity extends BaseActivity {



    private TextView detailtitle;
    private ImageView detailback;
    private TextView detailname;
    private TextView detailzfcard;
    private TextView detailmomey;
    private TextView detailfate;
    private TextView detailstate;
    private TextView detailtime;
    private TextView detailshoucard;
    private TextView detaildaomomey;
    GhtZd ghtZd;
    String user_id;
    int way_id;
    Dialog mydialog;
    @Override
    public void setContentView() {
        setContentView(R.layout.activity_detail_dh);

    }

    @Override
    public void initView() {
        this.detailtime = (TextView) findViewById(R.id.detail_time);
        this.detailstate = (TextView) findViewById(R.id.detail_state);
        this.detailfate = (TextView) findViewById(R.id.detail_fate);
        this.detailmomey = (TextView) findViewById(R.id.detail_momey);
        this.detaildaomomey = (TextView) findViewById(R.id.detail_daomomey);
        this.detailzfcard = (TextView) findViewById(R.id.detail_zfcard);
        this.detailshoucard = (TextView) findViewById(R.id.detail_shoucard);
        this.detailname = (TextView) findViewById(R.id.detail_name);
        this.detailback = (ImageView) findViewById(R.id.detail_back);
        this.detailtitle = (TextView) findViewById(R.id.detail_title);
    }

    @Override
    public void initData() {
        ghtZd = (GhtZd)getIntent().getSerializableExtra("detail");
        user_id=((MyApp)getApplication()).getDatas_load().getId();
        way_id = getIntent().getIntExtra ("way_id", 0);
        Log.e("DetailDhActivity",way_id+"");
    }

    @Override
    public void initActivity() {
        detailtime.setText(Util.getStrTi(ghtZd.getCreate_time()));
        String tip;
        switch (ghtZd.getStatus()) {
            case "00":
                tip="成功到账";
                break;
            case "03":
                tip="支付中";
                break;
            case "04":
                tip="支付成功";
                break;
            case "05":
                tip="支付失败";
                break;
            case "99":
                tip="到账失败";
                mydialog=Util.createLoadingDialog(DetailDhActivity.this);
                zf();
                break;
            default:
                tip="到账失败";
                break;
        }
        detailstate.setText(tip);
        detailfate.setText(Float.valueOf(ghtZd.getRate())*100+"%+"+ghtZd.getFee());
        detailmomey.setText(Util.twoString(ghtZd.getAmount())+"元");
        detaildaomomey.setText(Util.twoString(ghtZd.getCurr_money())+"元");
        if(ghtZd.getType().equals("0")){
            detailtitle.setText("代还详情");
        }else if(ghtZd.getType().equals("1")){
            detailtitle.setText("快捷详情");
        }
        detailzfcard.setText(ghtZd.getCard_no().substring(0, 4)+"    "+"***"+"    "+"***"+"    "+"***"+"    "+ghtZd.getCard_no().substring(ghtZd.getCard_no().length()-3,ghtZd.getCard_no().length()));
        detailshoucard.setText(ghtZd.getOther_card_no().substring(0, 4)+"    "+"***"+"    "+"***"+"    "+"***"+"    "+ghtZd.getOther_card_no().substring(ghtZd.getOther_card_no().length()-3,ghtZd.getOther_card_no().length()));
        MyApp application = (MyApp) getApplication();
        detailname.setText(application.getDatas_load().getUser_nicename());
        Log.e("快捷详情",ghtZd.toString());
        detailback.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
    // 高汇代付接口
    public void  zf(){
        AsyncHttpClient ahc = new AsyncHttpClient();
        RequestParams params = new RequestParams();
        params.put("user_id",user_id);
        params.put("way",way_id);
        params.put("card_no",ghtZd.getOther_card_no()); //付款卡
        params.put("orderNo",ghtZd.getOrder_no());
        Log.e("高汇通高汇代付接口params",params.toString());
        //url:   parmas：请求时携带的参数信息   responseHandler：是一个匿名内部类接受成功过失败
        ahc.post(Constant.GHTDF_URL, params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int i, Header[] headers, byte[] bytes) {
                closeLoadDial(mydialog);
                String result = null;
                try {
                    result = new String(bytes, "utf-8");
                    //                        Toast.makeText(SeletCardActivity.this, result, Toast.LENGTH_SHORT).show();
                    Log.e("高汇通高汇代付接口result", result);
                    try {
                        JSONObject jsonObject =new JSONObject(result);
                        if (jsonObject.get("flag").equals("success")) {
                            String info = jsonObject.getString("info");
                            if (jsonObject.has("data")){
                                info=jsonObject.getJSONObject("data").getJSONObject("data").getString("dealmsg");
                            }
                            tishi(info);
                        } else {
                            String info = jsonObject.getString("info");
                            if (jsonObject.has("data")){
                                info=jsonObject.getJSONObject("data").getJSONObject("data").getString("dealmsg");
                            }
                            tishi(info);
                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                closeLoadDial(mydialog);
            }
        });
    }
}
